package org.sloth.mx.tip
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	
	import mx.core.Application;
	import mx.events.ToolTipEvent;

	/**
	 * 提示管理器（需要调用静态方法<code>initialize</code>），需要显示提示的组件只需要实现接口<code>ITipClient</code>即可
	 * 
	 */	
	public class TipMgr
	{
//============================================================================
//PROPERTY
//============================================================================
		private static var _initialized:Boolean = false;
//============================================================================
//PUBLIC
//============================================================================
		/** 初始化提示管理器 */
		public static function initialize():void
		{
			if (_initialized) return;
			_initialized = true;
			
			var app:Application = Application.application as Application;
			if (app.stage == null)
			{
				app.addEventListener(Event.ADDED_TO_STAGE, appAddedToStage_handler);
				return;
			}
			initializeClients();
		}
//============================================================================
//PRIVATE
//============================================================================
		private static function initializeClients():void
		{
			var app:Application = Application.application as Application;
			
			app.stage.addEventListener(Event.ADDED, stageAdded_handler);
			app.stage.addEventListener(Event.REMOVED, stageRemoved_handler);
			
			var index:int;
			var child:DisplayObject;
			var list:Array = [app.stage];
			while (list.length > 0)
			{
				child = list.shift();
				if (child is DisplayObjectContainer)
				{
					index = DisplayObjectContainer(child).numChildren - 1;
					while (index > -1)
					{
						list.push(DisplayObjectContainer(child).getChildAt(index--));
					}
				}
				if (child is ITipClient) addTipClient(child as ITipClient);
			}
		}
		private static function addTipClient(client:ITipClient):void
		{
			if (client.toolTip == "" || client.toolTip == null) client.toolTip = " ";
			client.addEventListener(ToolTipEvent.TOOL_TIP_CREATE, toolTipCreate_handler);
		}
		private static function removeTipClient(client:ITipClient):void
		{
			client.removeEventListener(ToolTipEvent.TOOL_TIP_CREATE, toolTipCreate_handler);
		}
//============================================================================
//HANDLER
//============================================================================
		private static function appAddedToStage_handler(event:Event):void
		{
			var app:Application = Application.application as Application;
			app.removeEventListener(Event.ADDED_TO_STAGE, appAddedToStage_handler);
			
			initializeClients();
		}
		private static function stageAdded_handler(event:Event):void
		{
			var client:ITipClient = event.target as ITipClient;
			if (client != null) addTipClient(client);
		}
		private static function stageRemoved_handler(event:Event):void
		{
			var client:ITipClient = event.target as ITipClient;
			if (client != null) removeTipClient(client);
		}
		private static function toolTipCreate_handler(event:ToolTipEvent):void
		{
			var tipClient:ITipClient = event.currentTarget as ITipClient;
			event.toolTip = tipClient.tipUI;
		}
	}
}